package com.esaheki.flashcards.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.*;
import org.jdesktop.layout.GroupLayout;
import org.jdesktop.layout.LayoutStyle;

import com.esaheki.flashcards.classes.*;
import com.esaheki.flashcards.dao.*;

/**
 *
 * @author esaheki
 */
public class EditCardForm extends JFrame implements ActionListener {
	
    /**
	 * 
	 */
	private static final long serialVersionUID = 2092016840407806523L;
	
	// Variables declaration - do not modify
    private JButton okButton;
    private JButton cancelButton;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JTextField frontTextField;
    private JTextField backTextField;
    private Card card;
    // End of variables declaration

    /** Creates new form AddCardForm */
    public EditCardForm(Card card) {
    	this.card = card;
        initComponents();
        this.setLocationRelativeTo(null);
    }

    private void initComponents() {

        frontTextField = new JTextField();
        backTextField = new JTextField();
        jLabel1 = new JLabel();
        jLabel2 = new JLabel();
        okButton = new JButton();
        cancelButton = new JButton();

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jLabel1.setText("Front"); // NOI18N
        jLabel2.setText("Back"); // NOI18N

        okButton.setText("OK"); // NOI18N
        okButton.addActionListener(this);
        cancelButton.setText("Cancel"); // NOI18N
        cancelButton.addActionListener(this);
        
        frontTextField.setText(card.getFront());
        backTextField.setText(card.getBack());

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(GroupLayout.LEADING, false)
                    .add(layout.createSequentialGroup()
                        .add(okButton)
                        .addPreferredGap(LayoutStyle.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(cancelButton))
                    .add(jLabel1)
                    .add(jLabel2)
                    .add(frontTextField, GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
                    .add(backTextField))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .add(4, 4, 4)
                .add(frontTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jLabel2)
                .addPreferredGap(LayoutStyle.RELATED)
                .add(backTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(GroupLayout.BASELINE)
                    .add(okButton)
                    .add(cancelButton))
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == okButton) {
			try {
				CardDAO cDao = new CardDAO();
				card.setFront(frontTextField.getText());
				card.setBack(backTextField.getText());
				cDao.updateCard(card);
				MainPanel.refreshTable();
				
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			this.dispose();
		}
		else if (e.getSource() == cancelButton) {
			this.dispose();
			
		}
		
	}

}
